<?php
defined('DEDE') or exit('forbidden');

class db {
	var $link;

	function db($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 1)
	{
		$this->__construct($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
	}

	function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 1)
	{
		if($pconnect)
		{
			if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw))
			{
				$this->errdisplay('can not connect to mysql server');
			}
		}else
		{
			if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw))
			{
				$this->errdisplay('can not connect to mysql server');
			}
		}

		if($this->version() > '4.1')
		{
			global $charset, $dbcharset;
			if(!$dbcharset && in_array(strtolower($charset), array('gb2312', 'gbk', 'big5', 'utf-8')))
			{
				$dbcharset = $charset;
			}
			$dbcharset = str_replace('-', '', $dbcharset);
			if($dbcharset)
			{
				mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->link);
			}

			if($this->version() > '5.0.1')
			{
				mysql_query("SET sql_mode=''", $this->link);
			}
		}

		if($dbname)
		{
			if(!@mysql_select_db($dbname, $this->link))
			{
				$this->errdisplay('can not use database');
			}
		}
	}

	function select_db($dbname) {
		return mysql_select_db($dbname, $this->link);
	}

	function query($sql, $type = '')
	{
		$func = $type == 'U_B' && @function_exists('mysql_unbuffered_query') ?
			'mysql_unbuffered_query' : 'mysql_query';

		if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
			$this->errdisplay('MySQL Query Error', $sql);
		}
		return $query;
	}

	function getone($sql)
	{
		$query=$this->query($sql,'U_B');
		$onerow =& mysql_fetch_array($query, MYSQL_ASSOC);
		return $onerow;
	}

	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function affected_rows() {
		return mysql_affected_rows($this->link);
	}

	function error() {
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}

	function errno() {
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}

	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
	}

	function num_rows($query) {
		$query = mysql_num_rows($query);
		return $query;
	}
	function num_fields($query) {
		return mysql_num_fields($query);
	}

	function free_result($query) {
		return mysql_free_result($query);
	}

	function insert_id() {
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}


	function version() {
		return mysql_get_server_info($this->link);
	}

	function close() {
		return mysql_close($this->link);
	}

	function errdisplay($message = '', $sql = '') {
		echo 'MESSAGE:<br />'.$message.'<br />SQL:<br />'.htmlspecialchars($sql);
		echo '<br />'.$this->errno().'<br />';
		echo $this->error();
	}
}

?>